package com.source.hotelbath.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.source.hotelbath.R;
import com.source.hotelbath.base.CustomerAdapter;
import com.source.hotelbath.fragment.LinenItemAdapter;
import com.source.hotelbath.model.GoodsListBean;
import com.source.hotelbath.net.CustomerRequestConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/24.
 */

public class LinenListAdapter extends CustomerAdapter {


    private int SUBMIT=0;
    private int ITEM=1;
    private Context mContext;

    private  List<GoodsListBean> commonGoods =new ArrayList<>();

    private  List<GoodsListBean> commons =new ArrayList<>();

    public LinenListAdapter(Context context, List<GoodsListBean> commonGoods) {
        this.mContext=context;
        this.commonGoods=commonGoods;
    }

    @Override
    public int getCount() {
        if (commonGoods.size()>0){
            return commonGoods.size()+1;
        }else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (commonGoods.size()==position){
            return SUBMIT;
        }else {
            return ITEM;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position)==ITEM){
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.linen_item, null);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                holder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
                holder.iv_delect= (ImageView) convertView.findViewById(R.id.iv_delect);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_name.setText(commonGoods.get(position).getGoodsName());
            holder.tv_price.setText(commonGoods.get(position).getWashPrice());
            if (commonGoods.get(position).getCheckType()==1){
                holder.iv_select.setImageResource(R.drawable.select);
            }else {
                holder.iv_select.setImageResource(R.drawable.un_select);
            }

            if ("1".equals(commonGoods.get(position).getConfigFlag())){
                //不可删除
                holder.iv_delect.setVisibility(View.INVISIBLE);
            }else {
                holder.iv_delect.setVisibility(View.VISIBLE);
            }

            holder.iv_delect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commonGoods.remove(position);
                    notifyDataSetChanged();
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        if (commonGoods.get(position).getCheckType()==0){
                            commonGoods.get(position).setCheckType(1);
                        }else{
                            commonGoods.get(position).setCheckType(0);

                        }
                    notifyDataSetChanged();
                    }

            });

        }else {
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_submit, null);
                holder.btn_sure = (Button) convertView.findViewById(R.id.btn_sure);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    commons.clear();
                    for (int i=0;i<commonGoods.size();i++){
                        if (commonGoods.get(i).getCheckType()==1){
                            commons.add(commonGoods.get(i));
                        }
                    }
                    Intent intent = new Intent(CustomerRequestConstants.CHOOSEGOODS);
                    intent.putParcelableArrayListExtra("commonGoods", (ArrayList<? extends Parcelable>) commons);
                    intent.putParcelableArrayListExtra("commonlist", (ArrayList<? extends Parcelable>) commonGoods);
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
                    ((Activity)mContext).finish();
                }
            });
        }



        return convertView;
    }

    class ViewHolder {

        TextView tv_name;
        TextView tv_price;
        ImageView iv_delect;

        ImageView iv_select;
        Button btn_sure;

    }
}
